<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
</head>

<body style="padding:8px;">
    <form class="layui-form" action="" onsubmit="return false">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" name="name" lay-vertype="tips" placeholder="请输入放映厅" lay-verify="required"
                    autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <input type="text" id="txttime" name="time" lay-vertype="tips" placeholder="请输入放映时间" lay-verify="required"
                    autocomplete="off" class="layui-input" lay-key="1">
            </div>
            <div class="layui-input-inline">
                <input type="text" name="price" lay-vertype="tips" placeholder="请输入票价" lay-verify="required|number"
                    autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="width:auto;">
                <button class="layui-btn" lay-submit="" lay-filter="add">添加场次</button>
            </div>
        </div>
    </form>
    <table id="tabScreen" lay-filter="tabScreen"></table>
    <script src="../layui/layui.js"></script>
    <script src="../js/jquery-1.12.4.min.js"></script>
    <script src="../js/common.js"></script>
    <script src="../js/lodash.min.js"></script>
    <script>
        let screens = [];
        function query(url) {
            const q = {};
            _.last(url.split("?")).split("&")
                .map(it => it.split("="))
                .forEach(([p, v]) => {
                    q[p] = decodeURIComponent(v);
                });
            return q;
        };
        const c = query(location.href);
        layui.use(["form", "layer", "table", "laydate"], function (data) {
            const form = layui.form;
            const table = layui.table;
            const layer = layui.layer
            const laydate = layui.laydate;
            laydate.render({
                elem: "#txttime",
                type: "time"
            })
            table.render({
                elem: "#tabScreen",
                url: "/api/MovieAndCinema/" + c.id,
                autoSort: true,
                page: true,
                autoSort: false,
                parseData: function (res) {
                    screens = res.data.screens;
                    return {
                        code: 0,
                        message: "",
                        data: res.data.screens
                    }
                },
                cols: [[
                    { field: 'screens', title: '放映厅名', templet: `<div>{{d.name}}</div>`, sort: true, align: 'center' },
                    { field: 'screens', title: '放映时间', templet: `<div>{{d.time}}</div>`, sort: true, align: 'center' },
                    { field: 'screens', title: '票价', templet: `<div>{{d.price}}</div>`, sort: true, align: 'center' },
                    { title: "操作", templet: "#shanchu", width: 130, align: 'center' },
                ]]
            })
            //监听提交
            form.on('submit(add)', async function (data) {
                screens.push(data.field);
                await $.ajax({
                    url: "/api/MovieAndCinema/" + c.id,
                    method: "put",
                    data: JSON.stringify(screens),
                    contentType: "application/json",
                    success: function (eq) {
                        layui.table.reload("tabScreen");
                    }
                });
            })
        })
    </script>
    <script type="text/html" id="shanchu">
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="shanchu('{{d._id}}',this)">删除</button>
        </div>
    </script>
    <script>
        async function shanchu(id, btn) {
            screens.forEach((e, i) => {
                if (e._id == id) {
                    screens.splice(i, 1);
                }
            })
            await $.ajax({
                url: "/api/MovieAndCinema/" + c.id,
                method: "put",
                data: JSON.stringify(screens),
                contentType: "application/json",
                success: function (eq) {
                    layer.msg('删除场次成功！', {
                        icon: 1,
                        time: 1000
                    }, function () {
                        layui.table.reload("tabScreen");
                        $(btn).parents("tr").remove();
                    });
                }
            });
        }
    </script>
</body>

</html>